#!/usr/bin/env python
"""A program to generate a compilation database by parsing build
commands on standard input or an input file.  It assumes that a single
line compiles a single source file and the clang compiler is used as
the compiler.
Note that commands for other compilers can be used here, but there is
no guarantee that the options are understood by tools that use the
database.
"""
import sys
import os
import argparse
import re
import json
import subprocess

if __name__ == '__main__':
    description = 'Generate a Clang compilation database from compiler commands.'

    default_compilers = [
        'cc',
        'c++',

        'clang',
        'clang++',

        'gcc',
        'g++',

        'cl',

        # for now
        '/usr/local/bin/clang-3.4',
        '/usr/local/bin/clang++-3.4'
    ]

    c_extensions = [
        '.c',
        '.h'
    ]

    cxx_extensions = [
        '.cpp',
        '.hpp',
        '.cc',
        '.hh',
        '.cxx',
        '.hxx',
        '.C',
        '.H',
    ]

    objc_extensions = [
        '.m'
    ]

    objcxx_extensions = [
        '.mm'
    ]

    default_extensions = \
        c_extensions + \
        cxx_extensions + \
        objc_extensions + \
        objcxx_extensions

    parser = argparse.ArgumentParser(description=description)
    parser.add_argument('--root-directory',
                        action='store',
                        default='',
                        help='The root directory for the project.')
    parser.add_argument('--compilers',
                        action='store',
                        default=default_compilers,
                        help='A list of additional compilers.')
    parser.add_argument('--extensions',
                        action='store',
                        default=default_extensions,
                        help='A list of additional filename extensions.')
    parser.add_argument('--build-tool',
                        action='store',
                        default='',
                        help='The build tool that generated the input.')
    parser.add_argument('--compile-command-regex',
                        action='store',
                        default='',
                        help='A regular expression to parse the command line into the compiler, flags, and filename.')
    parser.add_argument('--incremental',
                        action='store_true',
                        default=False,
                        help='Incrementally update existing database.')
    parser.add_argument('-o', '--output-filename',
                        action='store',
                        default='compile_commands.json',
                        help='The filename of the compilation database.')

    args = parser.parse_args()

    args.output_filename = os.path.abspath(args.output_filename)

    compile_command_regex = None
    if args.compile_command_regex == '':
        if args.build_tool in ['', 'make']:
            compile_command_regex = re.compile(r'^([^ ]+) .* ([^ ]+) +-o +[^ ]+ *$')
        elif args.build_tool == 'Boost.Build':
            compile_command_regex = re.compile(r'^"([^"]+)" .+ "([^"]+)"$')
    else:
        if args.build_tool != '':
            print('warning: regex overriding build tool option.')

        compile_command_regex = re.compile(args.compile_command_regex)

    assert compile_command_regex is not None

    # create the initial compilation database
    compilation_database = []
    if args.incremental:
        if os.path.exists(args.output_filename):
            with open(args.output_filename, 'r') as inputfp:
                compilation_database = json.load(inputfp)

    # generate a more efficient compilation map by filename
    compilation_map = {}
    for entry in compilation_database:
        f = entry['file']
        if not os.path.isabs(f):
            f = os.path.join(entry['directory'], f)
        compilation_map[f] = entry

    # parse the compilation log and update the compilation database
    for line in sys.stdin:
        line = line.strip()

        if line == '':
            continue

        m = compile_command_regex.match(line)
        if m is None:
            continue
        if len(m.groups()) != 2:
            continue

        compiler = m.group(1)
        filename = m.group(2)

        # check if it is a call to a compiler
        if compiler not in args.compilers:
            continue

        # check if the filename extension is supported
        n, e = os.path.splitext(filename)
        if e not in args.extensions:
            continue

        entry = {
            'directory': args.root_directory if args.root_directory != "" else os.getcwd(),
            'command': line,
            'file': filename
        }

        f = entry['file']
        if not os.path.isabs(f):
            f = os.path.join(entry['directory'], f)

        # check for directory/file already in database
        compilation_map[f] = entry

    # convert the compilation map to a compilation database, sorted by
    # the filename
    compilation_database = []
    for k in sorted(compilation_map.keys()):
        compilation_database.append(compilation_map[k])

    # print as json
    with open(args.output_filename, 'w') as outputfp:
        json.dump(compilation_database,
                  fp=outputfp,
                  indent=2,
                  sort_keys=True)

def trace(args):
    "Trace the compile command and get the raw compile log."

    command = args.command
    p = subprocess.Popen(command, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    p.wait()

    return p.returncode

def parse(args):
    proc_end = {}
    proc_run = {}
    fname = args.raw_database
    if args.startup_dir is not None:
        cwd = os.path.abspath(args.startup_dir)
    else:
        cwd = os.path.dirname(os.path.abspath(fname))
    parse_exec_trace(proc_run, fname, args.auto_sys_inc)
    fs = sys.stdout
    if args.output != "" and args.output != "-":
        fs = open(args.output, "w")
    
    for i in args.include:
        include_file.append(re.compile(i))
    for e in args.exclude:
        exclude_file.append(re.compile(e))
    for i in args.include_dir:
        include_dir.append(re.compile(i))
    for e in args.exclude_dir:
        exclude_dir.append(re.compile(e))
    proc_res = []
    print_exec_trace(proc_run, cwd, proc_res)
    json.dump(proc_res, fs)

def add_common_opts_parse(s):
    """add the opts for subcommand "parse" """
    s.add_argument(
        "--startup-dir", "-s",
        default = None,
        help = "the startup direcotry")
    s.add_argument(
        "--auto-sys-inc", "-a",
        default = False,
        action = "store_true",
        help = "auto detect the system include path")
    s.add_argument(
        "--include", "-i",
        metavar = "REGEX",
        default = [],
        action = "append",
        help = "include the file parten")
    s.add_argument(
        "--exclude", "-e",
        metavar = "REGEX",
        default = [],
        action = "append",
        help = "exclude the file pattern")
    s.add_argument(
        "--include-dir", "-I",
        metavar = "REGEX",
        default = [],
        action = "append",
        help = "include the dir paten")
    
def add_common_opts_trace(s):
    """add the opts for subcommand """
    s.add_argument(
        "command",
        metavar = "COMMAND",
        nargs = argparse.REMAINDER,
        help = "build command line")

def main():
    """The main function"""
    
    parser = argparse.ArgumentParser(description = "Generate the compile database from build")
    subparsers = parser.add_subparsers(metavar = "SUBCOMMAND")

    s = subparsers.add_parser(
        "run",
        help = "(Default) build and parse command",
        description = "Create a compilation by parse a build file")
    add_common_opts_parse(s)
    add_common_opts_trace(s)
    s.add_argument(
        "--output", "-o",
        default = "./compile_commands.json",
        help = "the compilation database json file")
    s.set_defaults(sourceType = "run")
    s.set_defaults(fun = run)

    # generate build process file
    s = subparsers.add_parser(
        "trace",
        help = "trace build command",
        description = "Create a compilation database by build file")
    s.add_argument(
        "--output", "-o",
        default = "./compile_commands.raw",
        help = "the build process output file")
    add_common_opts_trace(s)
    s.set_defaults(sourceType = "trace")
    s.set_defaults(fun = trace)

    # parse
    s = subparsers.add_parser(
        "parse",
        help = "parse the build file",
        description = "Create a compilation database from the build result")
    add_common_opts_parse(s)
    s.add_argument(
        "raw_database",
        default = "./compile_commands.raw",
        nargs = '?',
        help = "the raw database from build file")
    s.add_argument(
        "output",
        default = "./compile_commands.json",
        nargs = '?',
        help = "the output compiler database")
    s.set_defaults(sourceType = "parse")
    s.set_defaults(fun = parse)

    parser.set_defaults_subparser(len(os.sys.argv) < 1 and "-h" or "run")
    args = parser.parse_args()
    return args.fun(args) 
    

def set_default_subparser(self, name, args = None):
    subparser_found = False
    for arg in sys.argv[1:]:
        if arg in ['-h', '--help']:
            break
        else:
            for x in self._subparsers._actions:
                if not isinstance(x, argparse._SubParsersAction):
                    continue
                for sp_name in x._name_parser_map.keys():
                    if sp_name in sys.argv[1:]:
                        subparser_found = True
            if not subparser_found:
                if args is None:
                    sys.argv.insert(1, name)
                else:
                    args.insert(0, name)

if __name__ == "__main__":
    try:
        import argparse
        argparse.ArgumentParser.set_default_subparser = set_default_subparser
    except ImportError:
        import arg2opt as argparse
    sys.exit(main())